package rs.etf.es.visualization;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import rs.etf.es.algorithm.Node;
import rs.etf.es.algorithm.NodeAddressBook;

public class NewNodeDialog extends JDialog {

    private final JPanel contentPanel = new JPanel();

    private Node myNode;
    private JTextField txtNodeName;
    private JList<String> lstValues;
    private JTextField txtNewValue;

    private DefaultListModel<String> valuesModel;
    private JCheckBox chkRoot;

    /**
     * Create the dialog.
     */
    public NewNodeDialog() {
	setModalityType(ModalityType.APPLICATION_MODAL);
	setTitle("Create new node");
	setModal(true);
	setBounds(100, 100, 273, 188);
	getContentPane().setLayout(new BorderLayout());
	contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
	getContentPane().add(contentPanel, BorderLayout.CENTER);
	contentPanel.setLayout(null);

	txtNodeName = new JTextField();
	txtNodeName.setBounds(95, 30, 157, 20);
	contentPanel.add(txtNodeName);
	txtNodeName.setColumns(10);

	JLabel lblNodeName = new JLabel("Node name:");
	lblNodeName.setBounds(10, 33, 75, 14);
	contentPanel.add(lblNodeName);

	JScrollPane scrollPane = new JScrollPane();
	scrollPane.setBounds(10, 61, 100, 109);
	contentPanel.add(scrollPane);

	lstValues = new JList<String>();
	scrollPane.setViewportView(lstValues);

	txtNewValue = new JTextField();
	txtNewValue.setBounds(123, 61, 129, 20);
	contentPanel.add(txtNewValue);
	txtNewValue.setColumns(10);

	JButton btnAddValue = new JButton("Add Value");
	btnAddValue.addActionListener(new ActionListener() {
	    public void actionPerformed(ActionEvent e) {
		addValue();
	    }
	});
	btnAddValue.setBounds(120, 92, 132, 23);
	contentPanel.add(btnAddValue);

	JButton btnSave = new JButton("Save");
	btnSave.addActionListener(new ActionListener() {
	    public void actionPerformed(ActionEvent e) {
		save();
	    }
	});
	btnSave.setBounds(163, 147, 89, 23);
	contentPanel.add(btnSave);

	chkRoot = new JCheckBox("Is root");
	chkRoot.setBounds(155, 122, 97, 23);
	contentPanel.add(chkRoot);
	manualInit();
    }

    protected void save() {
	myNode.setName(txtNodeName.getText());

	ArrayList<String> values = new ArrayList<String>();
	for (Object i : valuesModel.toArray()) {
	    values.add((String) i);
	}

	myNode.setValues(values);

	NodeAddressBook.instance().addNode(myNode);
	if (chkRoot.isSelected()) {
	    NodeAddressBook.instance().setRoot(myNode);
	}

	this.setVisible(false);
    }

    protected void addValue() {
	valuesModel.addElement(txtNewValue.getText());
	txtNewValue.setText("");
    }

    private void manualInit() {
	this.setUndecorated(true);
    }

    public Node getMyNode() {
	return myNode;
    }

    public void setMyNode(Node myNode) {
	this.myNode = myNode;
    }

    public void setVisible(boolean display) {
	if (display) {
	    valuesModel = new DefaultListModel<String>();
	    lstValues.setModel(valuesModel);
	    txtNodeName.setText(myNode.getName());
	    if (NodeAddressBook.instance().getRoot() != null) {
		chkRoot.setVisible(false);
		chkRoot.setSelected(false);
	    } else {
		chkRoot.setVisible(true);
	    }
	}

	super.setVisible(display);
    }
}
